package queues;

import helpers.CrawlerHelper;
import helpers.CrawlerSiteHelper;
import helpers.NumberHelper;
import helpers.StringHelper;

import java.io.IOException;
import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.appengine.api.NamespaceManager;

import debugs.ArticleDebug;

import models.ArticleModel;

import search.ArticleSearchEngine;
import systems.Recently;
import systems.RecentlyTag;

import admin.models.AutoSiteModel;

import entities.Article;
import entities.ArticleSearch;
import entities.AutoSite;

public class GetArticle extends HttpServlet {
	public void doGet(HttpServletRequest request, HttpServletResponse response)
		throws IOException, ServletException {
		
		String language = (String) request.getAttribute("language");
		
		String aid = (String) request.getParameter("aid");
		String url = URLDecoder.decode(request.getParameter("url"));
		
		//Get auto site
		AutoSiteModel autoSiteModel = AutoSiteModel.getInstance();
		AutoSite autoSite = autoSiteModel.getSite(aid);
		
		ArticleModel articleModel = ArticleModel.getInstance();
		
		if (!articleModel.checkUrlExists(url, language)) {
			if (autoSite != null) {
				Article article = CrawlerSiteHelper.getArticle(url, autoSite);
				
				//Set language
				article.setLanguage(language);
				
				//Get tag
				String tags = CrawlerHelper.getTag(StringHelper.html2text(article.getContent()));
				List<String> keys = new ArrayList<String>();
				if (!tags.isEmpty()) {
					String[] k = tags.split(",");
					keys = new ArrayList<String>();
					for (int i = 0; i < k.length; i++) {
						if (!k[i].isEmpty() && k[i].length() > 2) {
							keys.add(k[i].trim());
						}
					}
				}
				article.setTags(tags);
				article.setKeywords(keys);
				article.setRate(NumberHelper.getRandomNumberFrom(2, 5));
				
				articleModel.insert(article);
				
				
				response.getWriter().println("Submit article success with id " + article.getId());
				
				//Update tag
				RecentlyTag.insert(article.getKeywords());
				
				//Update recently Cache
				if (article.getStatus() == 1) {
					Recently.update(article);
				}
				
			} else {
				response.getWriter().println("Not exists auto site " + aid);
			}
		} else {
			response.getWriter().println("Article Exists");
		}
		
		
		
	}
}
